<template>
  <el-dialog title="选择属性" :visible.sync="open" :style="{ 'max-height': '100vh' }" :close-on-click-modal="false" width="85%" append-to-body @close="cancel">
    <div>
      <attribute   ref="attribute"  @gridReloadNoId="gridReloadNoId" :modelId="queryParams.modelId" :configFieldType="queryParams.configFieldType" />

    </div>
    <div slot="footer" class="dialog-footer" style="margin-top:0px;">
      <el-button type="primary" @click="submitForm">保 存</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>

</template>

<script>
import {listSelectRefField, addRefField} from "@/api/form/field";
import attribute from "@/views/form/config/model/attribute";

export default {
  name: "vnSelectField",
  components: { attribute },
  dicts: ['basic_java_type', 'form_type_name', 'data_extend', 'logic_yes_no', 'field_type'],
  props: {
    isDesign: {
      type: Boolean,
      default: false
    }
  },
  data() {
    var ValidCode = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('代码不能为空'));
      } else {
        basicCodeValid(value).then(data => {
          if (data == '1') {
            callback(new Error('已经存在该代码'));
          } else {
            callback();
          }
        });
      }
    };
    return {
      isKnowledge: null,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      fieldList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        basicCode: null,
        basicName: null,
        modelId: null,
        dataExtend: null,
        attributeType: null,
        remarks: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        basicName: [
          { required: true, trigger: "blur", message: "名称不能为空" }
        ],dataExtend: [
          { required: true, trigger: "blur", message: "字段类型不能为空" }
        ],basicCode: [
          { validator: ValidCode, trigger: "blur"}
        ],
      },
    };
  },
  methods: {
    gridReloadNoId() {
      this.$emit('gridReloadNoId');
      this.$emit('getList');
    },

    showDialog(modelId) {
      this.queryParams.basicCode = null;
      this.queryParams.basicName = null;
      this.queryParams.dataExtend = null;
      this.queryParams.attributeType = null;
      if (this.$route.query.modelFlag == 1) {
        this.isKnowledge = '1';
      }
      this.open = true;
      this.queryParams.modelId = modelId;
      this.$nextTick(() => {
        this.$refs.attribute.resetQuery();
      });
    },

    showDialog1(modelId,type) {
      this.queryParams.basicCode = null;
      this.queryParams.basicName = null;
      this.queryParams.dataExtend = null;
      this.queryParams.attributeType = null;
      if (this.$route.query.modelFlag == 1) {
        this.isKnowledge = '1';
      }
      this.open = true;
      this.queryParams.modelId = modelId;
      this.queryParams.configFieldType=type;
      this.$nextTick(() => {
        this.$refs.attribute.resetQuery();
      });
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      listSelectRefField(this.queryParams).then(response => {
        this.fieldList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.basicCode = null;
      this.queryParams.basicName = null;
      this.queryParams.dataExtend = null;
      this.queryParams.attributeType = null;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 提交按钮 */
    submitForm() {


      this.ids=this.$refs.attribute.getSelectIds();


      if (this.ids != null && this.ids != '') {
        addRefField({refIds: this.ids, modelId:this.queryParams.modelId, isKnowledge:this.isKnowledge,configFieldType:this.queryParams.configFieldType}).then(response => {
          this.open = false;
          if (this.isDesign) {
            if (this.ids != null && this.ids.length > 0) {
              this.$emit("addDesignList", this.ids);
            }
          } else {
            this.$modal.msgSuccess("保存成功");
            this.$emit("getList");
            this.$emit("loadTree");
          }

          if(this.queryParams.configFieldType!=null&&this.queryParams.configFieldType!=''){
            this.$parent.gridReload(this.queryParams.modelId);
          }
        });
      }
    },
  }
};
</script>
<style scoped>
::v-deep .el-dialog__body{
  padding-bottom: 0;
  padding-top: 10px;
}
</style>
